# 导入tushare
import tushare as ts
import pandas as pd

# 初始化pro接口
pro = ts.pro_api('46b616e295c8d31f5a4b22891744af6414252720e80988442438a089')

# 拉取数据
df = pro.daily(**{
    "ts_code": "",
    "trade_date": "",
    "start_date": "20250301",  # 修改为10天前的日期
    "end_date": "20250320",
    "offset": "",
    "limit": ""
}, fields=[
    "ts_code",
    "trade_date",
    "open",
    "high",
    "low",
    "close",
    "pre_close",
    "change",
    "pct_chg",
    "vol",
    "amount"
])

# 打印前几行数据以检查
print(df.head())

# 统计分析
# 计算每个股票的平均值
stats_summary = df.groupby('ts_code').agg({
    'open': 'mean',
    'high': 'mean',
    'low': 'mean',
    'close': 'mean',
    'vol': 'mean',
    'amount': 'mean',
    'pct_chg': 'mean'
}).reset_index()

# 打印统计结果
print(stats_summary)

# 分析涨跌幅的分布
pct_chg_distribution = df['pct_chg'].describe()
print(pct_chg_distribution)

# 可以绘制一些图表来可视化数据特点（可选）
import matplotlib.pyplot as plt

# 涨跌幅的直方图
plt.hist(df['pct_chg'], bins=50, alpha=0.7, color='blue')
plt.title('涨跌幅分布')
plt.xlabel('涨跌幅 (%)')
plt.ylabel('频数')
plt.show()